﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevComponents.DotNetBar;

namespace Hotel.QLPhong
{
    public partial class QL_phong : Form
    {
        public QL_phong()
        {
            InitializeComponent();
        }
        private SqlConnection con;
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string getdata(string sql, string x,int vt)
        {
            SqlDataReader rdr = null;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    if (rdr[0].ToString() == x) return rdr[vt].ToString();
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("loi");
            } 
            return "";
        }
        ListViewItem lvitem;
        public void QL_phong_Load(object sender, EventArgs e)
        {
            connect();
            SqlDataReader rdr = null;
            try
            {
                SqlCommand cmd = new SqlCommand("select * from Phong where DaXoa = '0'", con);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    lvitem = new ListViewItem(rdr[0].ToString(),getimagephong(rdr[6].ToString().Substring(0,1)));
                    lvphong.Items.Add(lvitem);
                    lvitem.ToolTipText = "Số giường: " + rdr[2].ToString() + "\n" + "Sức chứa: " + rdr[3].ToString() + "\n" + rdr[4].ToString() + "\n" + "Giá: "
                        + rdr[5].ToString() + "\n" + "Tiện nghi: " + rdr[7].ToString();
                }

            }
            catch (Exception)
            {
                MessageBox.Show("loi");
            } 
        }
        int getimagephong(string phong)
        {
            switch (phong)
            {
                case "0": return 0;
                case "1": return 1;
                default: return 2;
            }
        }

        OptionPhong frmoptionphong = null;
        PhongDangThue frmphongdangthue = null;
        string ttphong = "";
        private void lvphong_ItemActivate(object sender, EventArgs e)
        {
            string mp = lvphong.SelectedItems[0].Text;
            connect();
            ttphong = getdata("select * from Phong where DaXoa = '0'", mp, 6);
            if (ttphong.Substring(0, 1).Equals("1"))
            {
                frmphongdangthue = new PhongDangThue(mp);
                frmphongdangthue.ShowDialog(this);
            }
            else
            {
                frmoptionphong = new OptionPhong(mp);
                frmoptionphong.ShowDialog(this);
            }

            lvphong.Clear();
            QL_phong_Load(sender,e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = label1.Text.Substring(1, label1.Text.Length - 1) + label1.Text.Substring(0, 1);
        }
        private void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi");
            }
        }

        Themphong frmthemphong = null;
        private void btnthemphong_Click(object sender, EventArgs e)
        {
            frmthemphong = new Themphong();
            frmthemphong.ShowDialog(this);

            lvphong.Clear();
            QL_phong_Load(sender, e);
        }
    }
}
